package cn.xiaopengstack.types.event;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.RandomStringUtils;

import java.util.Date;

/**
 * @author jiangyangang
 */
public abstract class BaseEvent<T> {

    protected EventMessage<T> message;

    protected T data;

    protected String key;

    public BaseEvent(T data, String key) {
        this.data = data;
        this.key = key;
        buildEventMessage(data, key);
    }

    protected void buildEventMessage(T data, String key) {
        EventMessage<T> tEventMessage = new EventMessage<>();
        tEventMessage.setMessage(data);
        tEventMessage.setKey(key);
        tEventMessage.setSendDate(new Date());
        tEventMessage.setId(RandomStringUtils.randomNumeric(11));
        load(tEventMessage);
    }

    protected void load(EventMessage<T> message){
        this.message = message;
    }

    public abstract String topic();

    public EventMessage<T> payload(){
        return this.message;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder

    public static class EventMessage<T>{
        private String id;

        private String key;

        private Date sendDate;

        private T message;
    }
}
